package com.gprinter.http;

import android.content.Context;
import android.util.Log;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by 他的猫 on 2017/5/10.
 */

public class Okhttp {


    public static void post(String url, Map<String, String> paramters,
                            final Objectcallback callback) {
        try {
            OkHttpUtils
                    .post()
                    .url(url)
                    .params(paramters)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Log.e("888", "失败！" + e);
                            callback.onFalia(id, e.toString());


                        }

                        @Override
                        public void onResponse(String responseString, int id) {
                            if (callback != null) {
                                callback.onsuccess(responseString);
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static void get(String url, Map<String, String> paramters,
                           final Objectcallback callback) {
        try {
            OkHttpUtils
                    .get()
                    .url(url)
                    .params(paramters)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            callback.onFalia(id, e.toString());
                        }

                        @Override
                        public void onResponse(String responseString, int id) {
                            if (callback != null) {
                                callback.onsuccess(responseString);
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static void multiFileUpload(String url, Map<String, String> paramters, String file,String filename, final Objectcallback callback) {
        try {
            OkHttpUtils.post()//

//                .addFile("mFile", "server_afu.png", file)//
                    .addFile("file", filename, new File(file))
                    .url(url)
                    .params(paramters)
                    .build()//
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            callback.onFalia(id, e.toString());
                        }

                        @Override
                        public void onResponse(String responseString, int id) {
                            if (callback != null) {
                                callback.onsuccess(responseString);
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface Objectcallback {
        void onsuccess(String st);

        void onFalia(int code, String errst);

    }


    public static void upLoadToServer(  final String url, final Map<String, String> params, final ArrayList<String> list,  final Objectcallback callback) {

            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Disposition", "form-data;filename=enctype");
            PostFormBuilder builder = OkHttpUtils.post();
            builder.url(url);
            for (int i = 0; i < list.size(); i++) {
                File file = new File(list.get(i));
                if (!file.exists()) {

                    return;
                }
                String filename = file.getName();
                builder.addFile("mFile"+i, filename, file);
            }

            builder.params(params)
                    .headers(headers)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            callback.onFalia(id, e.toString());
                        }

                        @Override
                        public void onResponse(String responseString, int id) {
                            if (callback != null) {
                                callback.onsuccess(responseString);
                            }
                        }
                    });
        }





}
